package com.manager.controller;

import com.manager.domain.Student;
import com.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    Scanner sc = new Scanner(System.in);
    StudentService studentService = new StudentService();
    public void start() {
        int input;
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            input = sc.nextInt();
            switch (input) {
                case 1:
                    //添加学生
                    addStudent();
                    break;
                case 2:
                    //删除学生
                    delStudent();
                    break;
                case 3:
                    //修改学生
                    upDataStudent();
                    break;
                case 4:
                    //查看学生
                    findAllStudent();
                    break;
                case 5:
                    System.out.println("欢迎下次使用学生管理系统!");
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }

    }

    private void findAllStudent() {
        Student[] stus = studentService.findAllStudent();
        if (stus == null) {
            System.out.println("暂无信息 请添加后重试");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stus1 = stus[i];
            if (stus1 != null) {
                System.out.println(stus1.getId() + "\t" + stus1.getName() + "\t" + stus1.getAge() + "岁\t" + stus1.getBirthday());
            }
        }
    }

    private void upDataStudent() {
        String id = getString();
        if (id != null) {
            Student student = getStudent(id);
            studentService.upDataStudent(student, id);
            System.out.println("更新成功");
        }
    }

    private void delStudent() {
        String id = getString();
        if (id != null) {
            studentService.delStudent(id);
            System.out.println("删除成功");
        }
    }

    private String getString() {
        String id;
        while (true) {
            System.out.println("请输入id");
            id = sc.next();
            if (id.equals("0")) {
                return null;
            }
            boolean falg = studentService.isExists(id);
            if (falg) {
                break;
            }
            System.out.println("id不存在 请重新输入  退出[0]");
        }
        return id;
    }

    private void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入id");
            id = sc.next();
            if (id.equals("0")){
                return;
            }
            boolean falg = studentService.isExists(id);
            if (!falg) {
                break;
            }
            System.out.println("id已存在 请重新输入  退出[0]");
        }
        Student stu = getStudent(id);
        boolean isBool = studentService.addStudent(stu);
        if (isBool) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    private Student getStudent(String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }
}
